<?php
/**
* 将threshold,caculate,draw方法封装
* @deprecated
* @param string $year
* @param string $month
* @return array
*/
class Calendar
{
	
	function threshold($year,$month){
		$firstDay = mktime(0,0,0,$month,1,$year);    //返回日期的UNIX时间戳  mktime(hour,minute,second,month,day,year,is_dst);
		$lastDay = strtotime('+1 month -1 day',$firstDay);  //将任何英文文本的日期或时间描述解析为UNIX时间戳  strtotime(time,now);
		$days = date("t",$firstDay);   //取得当前天数
		$firstDayOfWeek = date("N",$firstDay);   //获得第一天是周几
		$lastDayOfWeek = date('N',$lastDay);   //获得最后一天是周几

		$lastMonthDate = strtotime('+1 day',$firstDay);  //上个月最后一天
		$lastMonthOfLastDay = date('d',$lastMonthDate);

		$nextMonthDate = strtotime('+1 day',$lastDay);   //下个月第一天
		$nextMonthOffirstDay = strtotime('+1 day',$lastDay);

		//日历的第一个日期
		if($firstDayOfWeek==7){                
			$firstDate = $firstDay;
		}else{
			$firstDate = strtotime('-'.$firstDayOfWeek.'day',$firstDay);
		}

		//日历的最后一个日期
		if($lastDayOfWeek==6){
			$lastDate = $lastDay;
		}elseif($lastDayOfWeek==7){
			$lastDate = strtotime('+6 day',$lastDay);
		}else{
			$lastDate = strtotime('+'.(6-$lastDayOfWeek).'day',$lastDay);
		}

		return array(
			'days'=>$days,
			'firstDayOfWeek'=>$firstDayOfWeek,
			'lastDayOfWeek'=>$lastDayOfWeek,
			'lastMonthOfLastDay'=>$lastMonthOfLastDay,
			'firstDate'=>$firstDate,
			'lastDate'=>$lastDate,
			'year' =>$year,
			'month'=>$month
		);
	}

   
	function caculate($calendar){
		$days = $calendar['days'];
		$firstDayOfWeek = $calendar['firstDayOfWeek'];  //本月第一天的星期
		$lastDayOfWeek = $calendar['lastDayOfWeek'];   //本月最后一天的星期
		$lastMonthOfLastDay = $calendar['lastMonthOfLastDay'];   //上个月的最后一天
		$year = $calendar['year'];
		$month = $calendar['month'];
		$dates = array();

		if($firstDayOfWeek!=7){
			$lastDays =array();
			$current = $lastMonthOfLastDay;  //上个月最后一天
			for($i=0;$i<$firstDayOfWeek;$i++){
				array_push($lastDays,$current);  //添加上一个月的日期天数
				$current--;
			}
			$lastDays =array_reverse($lastDays);  //反序
			foreach ($lastDays as $index => $day) {
				array_push($dates,array('day'=>$day,'tdclass'=>($index==0?'rest':''),'pclass'=>'outter'));
			}
		}

		//本月日历信息
		for($i=1;$i<=$days;$i++){
			$isRest = $this->_checkIsRest($year, $month, $i);
			array_push($dates,array('day'=>$i,'tdclass'=>($isRest ? 'rest' : ''),'pclass'=>''));     //array_push()  向第一个参数的数组尾部添加一个或多个元素，然后返回数组的长度
		}

		//下一个月的日历信息
		if($lastDayOfWeek==7){    //如果最后一天是周天
			$length=6;
		}elseif ($lastDayOfWeek==6) {   //如果最后一天是周六
			$length=0;
		}else{
			$length=6-$lastDayOfWeek;
		}
		for($i=1;$i<=$length;$i++){
			array_push($dates,array('day'=>$i,'tdclass'=>($i==$length ? 'rest' : ''),'pclass'=>'outter'));
		}
		return $dates;
	}

	// 判断是否是休息天
	private function _checkIsRest($year,$month,$day){
		$date = mktime(0,0,0,$month,$day,$year);
		$week = date("N",$date);
		return $week == 7 || $week == 6;
	}

	 function draw($caculate) { //$caculate 通过caculate方法计算后的数据
		 $tr = array();
		 $length = count($caculate);
		 $result = array();
		 foreach ($caculate as $index => $date) {
		 if($index % 7 == 0) {//第一列
		 $tr = array($date);
		 }elseif($index % 7 == 6 || $index == ($length-1)) {
		 array_push($tr, $date);
		 array_push($result, $tr);//添加到返回的数据中
		 $tr = array();//清空数组列表
		 }else {
		 array_push($tr, $date);
		 }
		 }
		 return $result;
		}
}
?>
